vec3 normal, lightDir;
vec4 diffuseCol, ambientCol, ambientGlobalCol,specularCol;
float NdotL;	//==cos(N,L)
float NdotHV;	//for specular component, HV = half vector

void main(){
	
	specularCol = vec4(0.0,0.0,0.0,0.0);	//init specularCol in case it isn't used
	
	normal = normalize(gl_NormalMatrix * gl_Normal);
	
	lightDir = normalize(vec3(gl_LightSource[0].position));		//position = direction in case of dir light
	
	//diffuse term according to lambert equation: Io=Ld*Md*cos(theta)
	//cos(theta):
	NdotL = max(dot(normal, lightDir), 0.0);	//dotProduct = cos
	//diffuse term:
	diffuseCol = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; //Backmaterial also usable, gl_FrontMaterial is predefined
	
	//ambient light term: Ia = Ga * Ma + La * Ma
	//local ambient component
	ambientCol =  gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
	//global ambient component
	ambientGlobalCol = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	

	//specular light according to Blinn/Phong model
	//only if NdotL > 0.0
	if(NdotL > 0.0){
		NdotHV = max(dot(normal,gl_LightSource[0].halfVector.xyz), 0.0);		//half vector doesn't has to be computed, already defined in lightsource, just use x y z, no w
		
		//use material and light values for computation of specComponent
		specularCol = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess);
		
	}
	
	//col = Idiff + Iamb + Ispec
	gl_FrontColor = NdotL * diffuseCol + (ambientCol + ambientGlobalCol) + specularCol;
	
	gl_Position = ftransform();	//don't alter vertex position
	
}